﻿#region Header

// Majesty of Omega - The next evolution of macro management space strategy
// 
// Copyright 2007-2009 André Claaßen
// 
// This file is protected by the terms and conditions of the
// Microsoft Community License (Ms-CL 1.1), a copy of which should
// have been distributed along with this software. If not,
// you may find the license information at the following URL:
// 
// http://www.microsoft.com/resources/sharedsource/licensingbasics/communitylicense.mspx

#endregion

#region

using System;
using System.Windows;
using System.Windows.Controls;
using Majesty_of_Omega.GUI.Controller;
using Majesty_of_Omega.Model.Research;

#endregion

namespace Majesty_of_Omega.GUI.Pages.Game.Research
{
    /// <summary>
    /// Research Page of Majesty of Omega. the user can spend research points on the various technology schools
    /// </summary>
    public partial class ResearchPage
    {
        /// <summary>
        /// Access to internal state of this page for unit testing purpooses
        /// </summary>
        public enum InternalValue
        {
            /// <summary>
            /// Retrieve the selected value from the <see cref="ResearchPage.TechFieldListView"/>
            /// </summary>
            TechFieldListViewSelectedValue,
            /// <summary>
            /// Retrieve the selected value from the <see cref="ResearchPage.TechSchoolListView"/>
            /// </summary>
            TechSchoolListViewSelectedValue
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ResearchPage"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor will be used for editing purposes in expression blend. 
        /// A dummy <see cref="Controller" /> is bound to the DataContext of the LayoutGrid
        /// </remarks>
        public ResearchPage()
        {
            InitializeComponent();
            TechSchoolCursor.Visibility = Visibility.Hidden;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ResearchPage"/> class.
        /// </summary>
        /// <param name="controller">The _controller.</param>
        public ResearchPage(InGameController controller)
        {
            InitializeComponent();
            TechSchoolCursor.Visibility = Visibility.Hidden;
            LayoutRoot.DataContext = controller;
            TechnologyMatrix.SaveGame = controller.Game;
            controller.PageInitialized(this);
        }

        /// <summary>
        /// Gets the internal value for unit testing purposes.
        /// </summary>
        /// <param name="internalValue">The internal value.</param>
        /// <returns></returns>
        public object GetInternalValueForUnitTesting(InternalValue internalValue)
        {
            switch (internalValue)
            {
                case InternalValue.TechFieldListViewSelectedValue:
                    return TechFieldListView.SelectedValue;
                case InternalValue.TechSchoolListViewSelectedValue:
                    return TechSchoolListView.SelectedValue;
                default:
                    throw new ArgumentOutOfRangeException("internalValue");
            }
        }

        /// <summary>
        /// Gets the access to the research page controller.
        /// </summary>
        /// <value>The research page controller.</value>
        private ResearchPageController Controller
        {
            get
            {
                return (ResearchPageController) LayoutRoot.DataContext;
            }
        }

        /// <summary>
        /// Handles the SelectionChanged event of the TechSchoolList control and ensures, that the first
        /// Tech Field of the school is selected
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.SelectionChangedEventArgs"/> instance containing the event data.</param>
        private void TechSchoolList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (TechFieldListView == null) return;
            AdjustTechSchoolCursor();
            TechFieldListView.SelectedIndex = 0;
        }

        /// <summary>
        /// Sets the tech school cursor to the selected Technology School.
        /// The "Cursor" combines visually the selected Technology School
        /// with and Technology Field Panel.
        /// </summary>
        private void AdjustTechSchoolCursor()
        {
            TechSchoolCursor.Visibility = Visibility.Visible;
            var selectedContainer =
                (UIElement)TechSchoolListView.ItemContainerGenerator.ContainerFromIndex(TechSchoolListView.SelectedIndex);
            if (selectedContainer == null) return;
            var cursorPos = selectedContainer.TranslatePoint(new Point(0.0, 0.0), TheoreticaLayoutGrid);
            TechSchoolCursor.Height = selectedContainer.DesiredSize.Height;
            TechSchoolCursor.Margin = new Thickness(TechSchoolCursor.Margin.Left, cursorPos.Y, 0.0, 0.0);
        }

        /// <summary>
        /// Handles the Click event of the AddSelectedAvailableResearch control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void AddSelectedAvailableResearch_Click(object sender, RoutedEventArgs e)
        {
            Controller.AddSelectedAvailableResearch((AppliedResearchItem) AvailableResearchListView.SelectedItem);
        }

        /// <summary>
        /// Handles the OnLoaded event of the ResearchPage control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void ResearchPage_OnLoaded(object sender, RoutedEventArgs e)
        {
            AdjustTechSchoolCursor();
        }
    }
}